package com.weixin.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;

import com.frame.ar.model.JFinalModel;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;


/*******************************************
 * 作用描述：微信按钮菜单model
 *******************************************
 * @author LRC
 * @createDate 2015-9-20
 * @version v1.0 
 * @updateRecord 格式：时间，作者，修改说明
 *******************************************
 * 
 */
public class AppMenu extends JFinalModel<AppMenu>{

	public static final String STATE = "DMISWEIXIN000";
	public static AppMenu dao = new AppMenu();
	
	/*******************************************
	 * 功能描述：获取对应appCfgId的微信菜单列表
	 *******************************************
	 * @createDate 2015-9-20 by LRC
	 * @param appId
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public WxMenu getWxMenu(WxMpService wxMpService,Object appcfgId){
		String sql = "select * from "+getTableName()+" where appcfgId=? and mLevel=? order by orderid";
		String subSql = "select * from "+getTableName()+" where appcfgId=?  and mLevel=? and pid=? order by orderid";
		//构建第一层菜单
		List<AppMenu> list = find(sql, appcfgId,1);
		WxMenu menu = new WxMenu();
		List<WxMenuButton> btns = new ArrayList<WxMenuButton>();
		for(int i=0; i<list.size() && i<3; i++){
		//for(AppMenu m : list){
			AppMenu m = list.get(i);
			WxMenuButton btn = new WxMenuButton();
			if(m.getStr("name")==null)continue;
			btn.setName(m.getStr("name"));
			btn.setKey(m.getStr("key"));
			btn.setType(m.getStr("type"));
			//判断是否需要进行授权
			if("T".equals(m.getStr("oauth"))){
				String url = m.getStr("url");
				url = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_USER_INFO,STATE );
				btn.setUrl(url);
			}else{
				btn.setUrl(m.getStr("url"));
			}
			//获取子菜单
			Object id = m.get("id");
			List<AppMenu> subList = find(subSql, appcfgId,2,id);
			List<WxMenuButton> subBtns = new ArrayList<WxMenuButton>();
			for(int j=0; j<subList.size() && j<5; j++){
			//for(AppMenu _m :subList){
				AppMenu subM = subList.get(j);
				WxMenuButton subBtn = new WxMenuButton();
				subBtn.setKey(subM.getStr("key"));
				subBtn.setName(subM.getStr("name"));
				subBtn.setType(subM.getStr("type"));
				//判断是否需要进行授权
				if("T".equals(subM.getStr("oauth"))){
					String url = subM.getStr("url");
					url = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_USER_INFO,STATE );
					subBtn.setUrl(url);
				}else{
					subBtn.setUrl(subM.getStr("url"));
				}
				subBtns.add(subBtn);
			}
			btn.setSubButtons(subBtns);
			btns.add(btn);
		}
		menu.setButtons(btns);
		
		return menu;
	}
	
	/**
	 * 功能描述：
	 * @createDate 2015-9-20 by LRC
	 * @param appId
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 **
	 * 
	 */
	public List<Map<String,Object>> getWxMenuTree(String appId){
		
		
		return null;
	}
	/**
	 * 功能描述：获取子节点数
	 * @createDate 2015-9-20 by LRC
	 * @param id
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 **
	 * 
	 */
	public Integer getChildrenNum(Object id) {
		String sql = "select count(1) from "+getTableName()+" where pid=?";
		Long sn = Db.queryLong(sql, id);
		return sn==null ? 0 : sn.intValue();
	}
	
	/*******************************************
	 * 功能描述：重置菜单
	 * 
	 * @throws WxErrorException
	 ******************************************* 
	 * @createDate 2015-9-19 by LRC
	 * @updateRecord 格式：时间，作者，修改说明
	 ******************************************* 
	 * 
	 */
	public static void initMenu(WxMpService _wxMpService,Object _appcfgId) throws Exception {
		List<WxMenuButton> buttons = null;
		try {
			_wxMpService.menuDelete();
			WxMenu wxMenu = AppMenu.dao.getWxMenu(_wxMpService,_appcfgId);
			System.out.println(JsonKit.toJson(wxMenu));
			_wxMpService.menuCreate(wxMenu);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(" menuCreate error . buttons=" + buttons == null ? null : JsonKit.toJson(buttons));
		}
	}
}
